﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
        internal class Program
        {
                public static List<int> Left = new List<int>(50);
                public static List<int> Right = new List<int>(50);

                public static int LeftPosition = 7;
                public static int RightPosition = 40;

                public static TransportState TransportState = Test.TransportState.无坯;

                public static List<TransportState> TransportStateHisory = new List<TransportState>();

                private static void Main(string[] args)
                {
                        Left.Add(-1);
                        for (int i = 45; i > 1; i--)
                        {
                                Left.Add(i);
                        }
                        for (int i = 0; i < 61; i++)
                        {
                                Left.Add(2);
                        }
                        Left.Add(-1);

                        Right.Add(-1);
                        for (int i = 0; i < 61; i++)
                        {
                                Right.Add(45);
                        }
                        for (int i = 45; i > 1; i--)
                        {
                                Right.Add(i);
                        }
                        Right.Add(-1);

                        for (int i = 0; i < Left.Count; i++)
                        {
                                Console.WriteLine("{0:6}{1:D}{2:6}{3:D}", "Left   ", Left[i], "   Right   ", Right[i]);
                                Analyse(Left[i], Right[i]);
                        }

                        Console.ReadKey();
                }

                public static void Analyse(int left, int right)
                {
                        //Console.WriteLine("{0:6}{1:D}{2:6}{3:D}", "Left   ", left, "   Right   ", right);

                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("TransportState: " + TransportState);

                        sb.Append("History :");
                        foreach (var item in TransportStateHisory)
                        {
                                sb.Append(item);
                                sb.Append(" ");
                        }
                        Console.WriteLine(sb.ToString());

                        switch (TransportState)
                        {
                                case TransportState.无坯:

                                        if (TransportStateHisory.Count > 2)
                                        {
                                                //right to left,increase
                                                for (int i = 0; i < TransportStateHisory.Count - 2; i++)
                                                {
                                                        if (TransportStateHisory[i] == TransportState.坯头
                                                        && TransportStateHisory[i + 1] == TransportState.满坯
                                                                && TransportStateHisory[i + 2] == TransportState.坯尾)
                                                        {
                                                                Console.WriteLine("添加1根");
                                                        }

                                                        //left to right,decrease
                                                        if (TransportStateHisory[i] == TransportState.坯尾
                                                        && TransportStateHisory[i + 1] == TransportState.满坯
                                                                && TransportStateHisory[i + 2] == TransportState.坯头)
                                                        {
                                                                Console.WriteLine("删除1根");
                                                        }
                                                }
                                        }

                                        TransportStateHisory.Clear();

                                        if (left < RightPosition
                                                && left > -1
                                                && right > RightPosition)
                                        {
                                                TransportState = TransportState.坯头;
                                                TransportStateHisory.Add(TransportState);

                                                break;
                                        }

                                        if (left < LeftPosition
                                                && left > -1
                                                && right > LeftPosition)
                                        {
                                                TransportState = TransportState.坯尾;
                                                TransportStateHisory.Add(TransportState);

                                                break;
                                        }

                                        break;

                                case TransportState.坯头:

                                        if (left < LeftPosition
                                                && left > -1
                                                && right > RightPosition)
                                        {
                                                TransportState = TransportState.满坯;
                                                TransportStateHisory.Add(TransportState);
                                                break;
                                        }

                                        if (left == -1
                                                && right == -1)
                                        {
                                                TransportState = TransportState.无坯;
                                        }

                                        break;

                                case TransportState.满坯:

                                        if (left < LeftPosition
                                                && left > -1
                                                && right > LeftPosition
                                                && right < RightPosition)
                                        {
                                                TransportState = TransportState.坯尾;
                                                TransportStateHisory.Add(TransportState);
                                                break;
                                        }

                                        if (left < RightPosition
                                                && left > -1
                                                && right > RightPosition)
                                        {
                                                TransportState = TransportState.坯头;
                                                TransportStateHisory.Add(TransportState);

                                                break;
                                        }

                                        break;

                                case TransportState.坯尾:

                                        if (left == -1
                                                && right == -1)
                                        {
                                                TransportState = TransportState.无坯;
                                                break;
                                        }

                                        if (left < LeftPosition
                                                && left > -1
                                                && right > RightPosition)
                                        {
                                                TransportState = TransportState.满坯;
                                                TransportStateHisory.Add(TransportState);
                                                break;
                                        }
                                        break;

                                default: break;
                        }
                }
        }
}